/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BLL;

import DAL.ConnectUnit;
import DTO.WithDraw_DTO;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.HashSet;

/**
 *
 * @author Nguyen Phi Hung
 */
public class WithDraw_BLL {
    ConnectUnit connection;
    
    public WithDraw_BLL(){
        this.connection = DAL.DAL.getDAL();
    }
    
    public HashSet<WithDraw_DTO> getWithDraws(String condition, String OrderBy) throws Exception{
        HashSet<WithDraw_DTO> withdraws = new HashSet<WithDraw_DTO>();
        ResultSet result = this.connection.Select("WITHDRAW", condition, OrderBy);
        while(result.next()){
            WithDraw_DTO withdraw = new WithDraw_DTO();
            
            withdraw.setWithdrawID(result.getString("WithdrawID"));
            withdraw.setAmountOfMoney(result.getInt("AmountOfMoney"));
            withdraw.setBookID(result.getString("BookID"));
            withdraw.setCustomerName(result.getString("CustomerName"));
            withdraw.setIDNumber(result.getString("IDNumber"));
            withdraw.setWithdrawingDate(result.getDate("WithdrawingDate"));
            
            withdraws.add(withdraw);
        }
        return withdraws;
    }
    
    public HashSet<WithDraw_DTO> getWithDraws(String condition) throws Exception{
        return this.getWithDraws(condition, null);
    }
     
    public HashSet<WithDraw_DTO> getWithDraws() throws Exception{
        return this.getWithDraws(null, null);
    }
    
    public WithDraw_DTO getWithDraw_byWithdrawID(String ID) throws Exception{
        HashSet<WithDraw_DTO> withdraws = this.getWithDraws("WithdrawID="+ ID);
        if(withdraws.size()>0)
            return withdraws.toArray(new WithDraw_DTO[withdraws.size()])[0];
        return null;   
    }
    
    public void Insert(WithDraw_DTO info) throws Exception{
        HashMap<String, Object> map = new HashMap<String, Object>();
        
        map.put("WithdrawID", info.getWithdrawID());
        map.put("AmountOfMoney", info.getAmountOfMoney());
        map.put("BookID", info.getBookID());
        map.put("CustomerName", info.getCustomerName());
        map.put("IDNumber", info.getIDNumber());
        map.put("WithdrawingDate", info.getWithdrawingDate());
        
        String[] arr = {"WithdrawID","AmountOfMoney","BookID","CustomerName","IDNumber","WithdrawingDate"};
        this.connection.Insert("WITHDRAW", map, arr);
    }
    
    public void Update(WithDraw_DTO info) throws Exception{
        HashMap<String, Object> map = new HashMap<String, Object>();
        
        map.put("WithdrawID", info.getWithdrawID());
        map.put("AmountOfMoney", info.getAmountOfMoney());
        map.put("BookID", info.getBookID());
        map.put("CustomerName", info.getCustomerName());
        map.put("IDNumber", info.getIDNumber());
        map.put("WithdrawingDate", info.getWithdrawingDate());
        
        this.connection.Update("WITHDRAW", map, "WithdrawID="+ info.getWithdrawID());
    }
    
    public void Delete(WithDraw_DTO info) throws Exception{
        this.connection.Delete("WITHDRAW", "WithdrawID="+ info.getWithdrawID());
    }
}
